package cn.fintecher.pangolin.service.manage.service;

import cn.fintecher.pangolin.common.exception.BadRequestException;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.BeanUtils;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.Notice;
import cn.fintecher.pangolin.service.manage.elastic.NoticeElastic;
import cn.fintecher.pangolin.service.manage.request.AddNoticeRequest;
import cn.fintecher.pangolin.service.manage.request.FindNoticeRequest;
import cn.fintecher.pangolin.service.manage.request.ModifyNoticeRequest;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 10:02 2019/9/2
 */

@Service("noticeService")
@Transactional(rollbackFor = Exception.class)
public class NoticeService {
    @Autowired
    private NoticeElastic noticeElastic;

    @Autowired
    private BaseService baseService;

    /**
     * 多条件查询所有公告
     */
    public Page<Notice> findAllNotice(FindNoticeRequest request, Pageable pageable) {
        BoolQueryBuilder builder = request.getBuilder();
        SortBuilder sortBuilder = SortBuilders.fieldSort("operateTime").unmappedType("date").order(SortOrder.DESC);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).withPageable(pageable).withSort(sortBuilder).build();
        return noticeElastic.search(searchQuery);
    }

    /**
     * 新增公告
     */
    public void addNotice(AddNoticeRequest request, String token) {
        UserModel userModel = baseService.getUserByToken(token);
        Notice notice = new Notice();
        BeanUtils.copyPropertiesIgnoreNull(request, notice);
        notice.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
        noticeElastic.save(notice);
    }

    /**
     * 删除公告
     */
    public void deleteNotice(String id) {
        noticeElastic.deleteById(id);
    }

    /**
     * 修改公告
     */
    public void modifyNotice(ModifyNoticeRequest request, String token) {
        UserModel userModel = baseService.getUserByToken(token);
        Notice notice = noticeElastic.findById(request.getId()).orElseThrow(() -> new BadRequestException(null, "notice", "notice.is.null"));
        notice.setType(request.getType());
        notice.setTitle(request.getTitle());
        notice.setContent(request.getContent());
        notice.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
        noticeElastic.save(notice);
    }

    /**
     * 首页公告列表查询
     */
    public List<Notice> getNoticeList() {
        SortBuilder sortBuilder = SortBuilders.fieldSort("operateTime").unmappedType("date").order(SortOrder.DESC);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withSort(sortBuilder).build();
        Iterable<Notice> iterable = noticeElastic.search(searchQuery);
        return IterableUtils.toList(iterable);
    }
}
